/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.service.knowledge;

import cn.ac.nercis.pes.common.constant.evaluation.EvaluationConclusion;
import cn.ac.nercis.pes.common.constant.evaluation.EvaluationItem;
import cn.ac.nercis.pes.model.dto.common.Result;
import cn.ac.nercis.pes.model.dto.common.ResultCode;
import cn.ac.nercis.pes.model.dto.knowledge.KmExpectedResultDTO;
import cn.ac.nercis.pes.repository.dal.evaluation.AssetRepository;
import cn.ac.nercis.pes.repository.dal.knowledge.KmExpectedResultRepository;
import cn.ac.nercis.pes.repository.mapper.knowledge.KmExpectedResultMapper;
import cn.ac.nercis.pes.repository.model.evaluation.AssetDO;
import cn.ac.nercis.pes.repository.model.knowledge.KmExpectedResultDO;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 预期结果（知识库）服务
 *
 * @author zpy
 * @version 1.0.0
 */
@Slf4j
@Service
public class KmExpectedResultService {
    private static final Integer IMPORT_ROW_NUMBER = 3;
    private static final String IMPORT_SHEET_NAME = "预期结果信息";

    private final KmExpectedResultRepository expectedResultRepository;
    private final AssetRepository assetRepository;

    @Autowired
    public KmExpectedResultService(KmExpectedResultRepository expectedResultRepository, AssetRepository assetRepository) {
        this.expectedResultRepository = expectedResultRepository;
        this.assetRepository = assetRepository;
    }

    public Result<List<KmExpectedResultDTO>> findExpectedResults(@NonNull String layerTarget, @NonNull String unitTarget, @NonNull String assetId,
                                                                 @NonNull EvaluationItem evalItem, @NonNull EvaluationConclusion evalConclusion) {
        AssetDO assetDO = this.assetRepository.findById(assetId).orElse(null);
        if (Objects.isNull(assetDO)) {
            return Result.failed(ResultCode.ASSET_NOT_EXIST);
        }
        KmExpectedResultDO example = KmExpectedResultDO.builder()
                .layerTarget(layerTarget)
                .unitTarget(unitTarget)
                .assetClassId(assetDO.getClassId())
                .evalItem(evalItem)
                .evalConclusion(evalConclusion)
                .build();
        ExampleMatcher matcher = ExampleMatcher.matchingAll();
        List<KmExpectedResultDTO> resultDTOS = this.expectedResultRepository.findAll(Example.of(example, matcher))
                .stream()
                .map(KmExpectedResultMapper.INSTANCE::toDTO)
                .collect(Collectors.toList());
        return Result.success(resultDTOS);
    }

}